package com.longshen.dubbo.api.advice;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.longshen.dubbo.api.base.RestResult;
import com.longshen.dubbo.api.exception.DubboApiException;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 功能描述: 处理业务异常<br>
	 *
	 * @param ex
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ResponseStatus(value = HttpStatus.OK) // 500
	@ExceptionHandler(DubboApiException.class)
	@ResponseBody
	public RestResult handleException(DubboApiException ex) {
		RestResult rsp = RestResult.newInstance();
		rsp.setResultcode(ex.getErrorCode());
		rsp.setMessage(ex.getMessage());
		return rsp;
	}

	

	/**
	 * 功能描述: 处理通用异常<br>
	 *
	 * @param ex
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ResponseStatus(value = HttpStatus.OK) // 500
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public RestResult handleException(Exception ex) {
		log.error("admin controller error", ex);
		RestResult rsp =RestResult.newInstance();
		rsp.setResultcode("999999");
		rsp.setMessage("系统异常");
		return rsp;
	}

}
